package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.ConsumptionListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.ConsumptionBean;
import com.lc.luzhengtang.conn.ConnConsumption;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.lc.luzhengtang.R.id.text_normal_person;

/**
 * Created by Administrator on 2017/6/20.
 */
public class ConsumptionActiivty extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(text_normal_person)
    TextView textNormalPerson;
    @BindView(R.id.recycle_personal_list)
    RecyclerView recyclePersonalList;
    ConsumptionListAdapter consumptionListAdapter;

    List<ConsumptionBean.DataBeanX.DataBean> mData;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    private String getids;
    private int currentPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumption);
        ButterKnife.bind(this);
        recyclePersonalList.setLayoutManager(new LinearLayoutManager(this));
        getids = getIntent().getStringExtra("goodstype_id");
        textNormalPerson.setText(getIntent().getStringExtra("type_name"));
        mData = new ArrayList<>();
        initData();
        consumptionListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(ConsumptionActiivty.this, Personalconsumption.class));
            }
        });

//        personalData();
    }


    protected void initData() {
        recyclePersonalList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclePersonalList.setLayoutManager(new LinearLayoutManager(this));

        consumptionListAdapter = new ConsumptionListAdapter(null);
        consumptionListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        consumptionListAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(TeamActivityCommonListActivity.this, R.drawable.line_divider);
//        recyclerViewList.addItemDecoration(new SimpleDividerItemDecoration(TeamActivityCommonListActivity.this, drawable, 1));

        recyclePersonalList.setAdapter(consumptionListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        consumptionListAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }


    @OnClick({R.id.img_news, text_normal_person})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case text_normal_person:
                break;
        }
    }


    public void personalData() {

    }

    @Override
    public void onRefresh() {
      /*  consumptionListAdapter = new ConsumptionListAdapter(mData);
        recyclePersonalList.setAdapter(consumptionListAdapter);

*/

        new ConnConsumption(getIntent().getStringExtra("goodstype_id"), currentPage + "", new AsyCallBack<ConsumptionBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                consumptionListAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ConsumptionBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
//                text_normal_person

                mData = s.getData().getData();

                if (mData != null) {
//                    currentPage = 1;
                    consumptionListAdapter.setNewData(mData);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                setRefreshing(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                consumptionListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);

                consumptionListAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(ConsumptionActiivty.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnConsumption(getIntent().getStringExtra("goodstype_id"), currentPage + "", new AsyCallBack<ConsumptionBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                consumptionListAdapter.setEnableLoadMore(true);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ConsumptionBean s) throws Exception {
                super.onSuccess(toast, type, o, s);

             /*   mData = s.getData().getData();

                if (mData != null) {
//                    currentPage = 1;
                    consumptionListAdapter.setNewData(mData);
                }*/
                mData = s.getData().getData();

                if (mData != null && mData.size() > 0) {
                    currentPage++;
                    consumptionListAdapter.setNewData(mData);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    consumptionListAdapter.loadMoreComplete();
                    View noDataView = LayoutInflater.from(ConsumptionActiivty.this).inflate(R.layout.item_no_data, (ViewGroup) recyclePersonalList.getParent(), false);
                    consumptionListAdapter.addFooterView(noDataView);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                setRefreshing(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                consumptionListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);

                consumptionListAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(ConsumptionActiivty.this, false);
    }
}
